package com.yonghong.groupreport.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.yonghong.groupreport.domain.ReportResult;
import com.yonghong.groupreport.domain.ReportsParams;
import com.yonghong.groupreport.domain.DataSourceVo;
import com.yonghong.groupreport.service.ReportsResultService;
import com.yonghong.groupreport.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yonghong.groupreport.config.DynamicDataSource;

import java.util.List;
//import com.yonghong.groupreport.config.GetDataSource;

@RestController
@RequestMapping("/reports")
public class ReportsResultController {
    @Autowired
    private ReportsResultService reportsResultService;
@Autowired
private DataSourceVo dataSourceVo;
@Autowired
private DynamicDataSource dynamicDataSource;



    @GetMapping("/listReport")
    public R getList(@RequestBody String requst) throws NoSuchFieldException, IllegalAccessException {
        ReportsParams reportsParams =JSON.parseObject(requst,new TypeReference<ReportsParams>(){});
//!balanceSheetParams.getOrg_datatpye().equals("U6") &&
        if( reportsParams.getOrgDatatype()!=null) {
           // dataSourceVo.setDataType("U8");
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(reportsParams.getOrgDatatype());
           // System.out.println(balanceSheetParams);
        }
        List<ReportResult> list=null;
        //U6或u8
        if( (reportsParams.getOrgDatatype().equals("U6") || reportsParams.getOrgDatatype().equals("U8"))) {
           if(reportsParams.getPaName().equals("资产负债表") ) {
               list=reportsResultService.selectBalanceSheet(reportsParams);
           }else  if(reportsParams.getPaName().equals("利润表") )
           {
               list=reportsResultService.selectIncome(reportsParams);
               
           } else if (reportsParams.getPaName().equals("利润表（季度）")) {
               list=reportsResultService.selectIncomeQuarter(reportsParams);
           }
        }
        //NC
        if((reportsParams.getOrgDatatype().equals("NC"))){

           reportsResultService.selectReportDataFlag(reportsParams);
           if(reportsParams.getPFlag().equals("SUCCESS")){
               list=reportsResultService.selectReportData(reportsParams);
           }
        }
        if (list!=null){
            return R.success(list);
        }else {
            return R.error("数据异常");
        }
    }
//    @GetMapping("/listIncome")
//    public R getListIncome(@RequestBody String requst) throws NoSuchFieldException, IllegalAccessException {
//        ReportsParams reportsParams =JSON.parseObject(requst,new TypeReference<ReportsParams>(){});
//
//        if( reportsParams.getOrgDatatype()!=null) {
//            // dataSourceVo.setDataType("U8");
//            DynamicDataSource.clearDataSource();
//            // 切换数据源
//            dynamicDataSource.createDataSource(reportsParams.getOrgDatatype());
//            // System.out.println(balanceSheetParams);
//        }
//        return R.success(reportsResultService.selectIncome(reportsParams));
//    }
//
//    @GetMapping("/listIncomeQuarter")
//    public R getListIncomeQuarter(@RequestBody String requst) throws NoSuchFieldException, IllegalAccessException {
//        ReportsParams reportsParams =JSON.parseObject(requst,new TypeReference<ReportsParams>(){});
//
//        if( reportsParams.getOrgDatatype()!=null) {
//            // dataSourceVo.setDataType("U8");
//            DynamicDataSource.clearDataSource();
//            // 切换数据源
//            dynamicDataSource.createDataSource(reportsParams.getOrgDatatype());
//            // System.out.println(balanceSheetParams);
//        }
//        return R.success(reportsResultService.selectIncomeQuarter(reportsParams));
//    }
}
